package cn.knightzz.service.impl;

import cn.knightzz.constant.RoleType;
import cn.knightzz.entity.SysBuilding;
import cn.knightzz.entity.SysPopulation;
import cn.knightzz.entity.SysUser;
import cn.knightzz.mapper.SysPopulationMapper;
import cn.knightzz.service.SysPopulationService;
import cn.knightzz.service.SysUserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author knightzz
 * @since 2021-05-09
 */
@Service
public class SysPopulationServiceImpl extends ServiceImpl<SysPopulationMapper, SysPopulation> implements SysPopulationService {

    @Resource
    SysPopulationMapper populationMapper;

    @Resource
    SysUserService userService;

    /**
     * 添加人口信息
     *
     * @param population
     * @return
     */
    @Override
    public Boolean addPopulation(SysPopulation population) {
        try {
            return populationMapper.insert(population) != 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 更行人口信息
     *
     * @param population
     * @return
     */
    @Override
    public Boolean updatePopulation(SysPopulation population) {
        try {
            return populationMapper.updateById(population) != 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 删除人口信息
     *
     * @param id
     * @return
     */
    @Override
    public Boolean deletePopulation(Long id) {
        try {
            return populationMapper.deleteById(id) != 0;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 分页查询
     *
     * @param pageNum
     * @param pageSize
     * @param query
     * @return
     */
    @Override
    public IPage<SysPopulation> selectByPage(Long userId, Long pageNum, Long pageSize, String query) {

        Page<SysPopulation> page = new Page<>();
        page.setSize(pageSize);
        page.setCurrent(pageNum);

        // 获取当前用户信息
        SysUser user = userService.getUserById(userId);
        // 判断当前用户角色
        switch (user.getType().intValue()){
            // 超级管理员和网格员管理员获取当前辖区的数据
            case RoleType.SUPER_ADMIN:
            case RoleType.ADMIN:
                return populationMapper.selectByAdmin(page,query,user.getAddress());
            // 网格员和其他用户获取当前辖区并且自己上传的数据
            default:
                return populationMapper.selectByOther(page, query, user.getAddress(), userId);
        }
    }

    /**
     * 根据辖区获取
     *
     * @param area  当前辖区
     * @param query 详细地址,根据它来模糊查询
     * @return
     */
    @Override
    public List<SysPopulation> getPopulationByArea(String area, String query) {

        try {
            return populationMapper.getPopulationByAreaLikeAddress(area, query);
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }
}
